import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class DB
{
    /**
     * 数据库连接初始化
     */
    public static void init()
    {

	// 创建流读取对象
	DB db = new DB();
	InputStream input = db.getClass()
		.getResourceAsStream("conn.properties");
	Properties pres = new Properties();
	try
	{
	    pres.load(input);
	    // 连接池数据参数
	    ConnectionPool pool = new ConnectionPool();
	    pool.setMaxSize(Integer.parseInt(pres.getProperty("ConnMaxSize")));
	    pool.setInitSize(Integer.parseInt(pres.getProperty("ConnInitSize")));
	    pool.setIncreaseSize(Integer.parseInt(pres
		    .getProperty("ConnIncreaseSize")));
	    // 连接参数
	    pool.setURL(pres.getProperty("ConnUrl"));
	    pool.setUser(pres.getProperty("ConnUser"));
	    pool.setPassWord(pres.getProperty("ConnPassWord"));
	    // 连接超时
	    pool.setLeaveTime(Integer.parseInt(pres
		    .getProperty("ConnLeaveTime")));
	    // 间隔刷新
	    pool.setPoolRefTime(Integer.parseInt(pres
		    .getProperty("ConnPoolRefTime")));
	    // 连接异常超时回收
	    pool.setTimeOut(Integer.parseInt(pres.getProperty("ConnTimeOut")));
	    // 载入驱动
	    try
	    {
		Class.forName(pres.getProperty("ConnDriver"));
		// 初始化连接池
		pool.initPool();
	    } catch (ClassNotFoundException e)
	    {
		// TODO Auto-generated catch block
		System.out.println("加载驱动出错:" + e.toString());
	    }
	} catch (IOException e)
	{
	    // TODO Auto-generated catch block
	    System.out.println("读取配置出错:" + e.toString());
	}
	// 初始化连接池
    }

    /**
     * 返回一个连接
     */
    public static Connection getConn()
    {
	ConnectionPool pool = new ConnectionPool();
	return pool.getConnection();
    }

    /**
     * 将连接对象返回给连接池
     */
    public static void close(Connection conn)
    {
	ConnectionPool pool = new ConnectionPool();
	pool.returnConn(conn);
    }
}